/**
 * Created by winaya on 2016/11/30.
 */
$.extend(
    {
        /**
         * html字符串转码
         * @param str
         * @returns {string}
         */
        html_encode: function (str) {
            var s = "";
            if (str.length == 0) {
                return "";
            }
            s = str.replace(/&/g, "&gt;");
            s = s.replace(/</g, "&lt;");
            s = s.replace(/>/g, "&gt;");
            s = s.replace(/ /g, "&nbsp;");
            s = s.replace(/\'/g, "&#39;");
            s = s.replace(/\"/g, "&quot;");
            s = s.replace(/\n/g, "<br>");
            return s;
        },
        /**
         * 将html字符串解码
         * @param str
         * @returns {string}
         */
        html_decode: function (str) {
            var s = "";
            if (str.length == 0) {
                return "";
            }
            s = str.replace(/&gt;/g, "&");
            s = s.replace(/&lt;/g, "<");
            s = s.replace(/&gt;/g, ">");
            s = s.replace(/&nbsp;/g, " ");
            s = s.replace(/&#39;/g, "\'");
            s = s.replace(/&quot;/g, "\"");
            s = s.replace(/<br>/g, "\n");
            return s;
        },
        /**
         * 将字符串转成unicode编码
         * @param str
         * @returns {string}
         */
        encodeUnicode: function (str) {
            var res = [];
            for (var i = 0; i < str.length; i++) {
                res[i] = ( "00" + str.charCodeAt(i).toString(16) ).slice(-4);
            }
            return "\\u" + res.join("\\u");
        },
        /**
         * 将unicode编码转成字符串
         * @param str
         */
        decodeUnicode: function (str) {
            str = str.replace(/\\/g, "%");
            return unescape(str);
        }
    });